//
//  WKStatusCell.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/3/30.
//  Copyright © 2016年 恋guang年. All rights reserved.
//

import UIKit
import SnapKit
class WKStatusCell: UITableViewCell {
    //bottomView的顶部约束
    var boottomViewTopCon : Constraint?
    ///  MARK: - 微博属性 控制器把模型传给cell的时候cell再把模型传递给originalView让它给它子控件赋值
    var status:WKStatus? {
        didSet{
            originalView.status = status
            //先卸载bottomView的顶部约束
            boottomViewTopCon?.uninstall()
            if let retweetedStatus = status?.retweeted_status{
                retweetView.hidden = false
               // 将这个被转发的模型赋值给被转发的view
                retweetView.retweetStatus = retweetedStatus
                buttomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.boottomViewTopCon = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                retweetView.hidden = true
                buttomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.boottomViewTopCon = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier:reuseIdentifier )
        //设置cell背景
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        /// 添加原生View 添加约束
        prepareForUI()
    }
    
    /// 添加原生View 添加约束
    func prepareForUI(){
        //添加originalView 和bottomView
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(buttomView)
        
        
        //originalView添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            //顶部下移动一段距离
            make.top.equalTo(contentView).offset(10)
        }
        //retweetView添加约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        //buttomView添加约束
        buttomView.snp_makeConstraints { (make) -> Void in
            self.boottomViewTopCon = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.trailing.equalTo(originalView)
            make.height.equalTo(44)
        }
        // 3.当使用autolayout来约束cell里面的控件,并且cell的高度想通过autolayout来自动确定,需要给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.trailing.leading.equalTo(self)
            make.bottom.equalTo(buttomView.snp_bottom)
        }
    }
    
    
    //MARK: - 懒加载
    //原创微博View
    private lazy var originalView:WKStatusOriginalView = WKStatusOriginalView()
    //被转发微博View
    private lazy var retweetView:WKStatusRetweetView = WKStatusRetweetView()
    //底部按钮View
    private lazy var buttomView:WKStatusButtomView =  WKStatusButtomView()
    
    
}
